package cn.zzuisa.news.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;

import cn.zzuisa.news.entity.IprNewsInfo;
import cn.zzuisa.news.entity.IprNewsItem;
import cn.zzuisa.news.entity.IprNewsType;
import cn.zzuisa.news.entity.Page;
import cn.zzuisa.news.enums.CommonEnums;
import cn.zzuisa.news.form.IprNewsInfoForm;
import cn.zzuisa.news.form.IprNewsItemForm;
import cn.zzuisa.news.form.NewsEditForm;
import cn.zzuisa.news.service.IprNewsService;
import cn.zzuisa.news.service.impl.IprNewsServiceImpl;
import cn.zzuisa.news.util.JsonUtil;
import cn.zzuisa.news.util.ResultVOUtil;

@WebServlet(name = "indexnews", urlPatterns = "/indexnews", asyncSupported = true)
public class indexNewsServlet extends HttpServlet {
	private IprNewsService iprNewsServiceImpl = new IprNewsServiceImpl();

	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String method = req.getParameter("method");
		if (method == null) {
			List<IprNewsInfo> listinfo = iprNewsServiceImpl.listinfoByType(null);
			req.setAttribute("list", listinfo);
			req.getRequestDispatcher("WEB-INF/ftl/index/indexNews.ftl").forward(req, resp);

		} else if (method.equals("type")) {
			String key = req.getParameter("key");
			String doer = req.getParameter("doer");
			String id = req.getParameter("id");
			List<IprNewsType> listtype = iprNewsServiceImpl.listtype();
			req.setAttribute("types", listtype);
			
			
			
			if (key != null) {
				IprNewsType iprNewsType = iprNewsServiceImpl.findIprNewsTypeById(Integer.parseInt(key));
				req.setAttribute("type", iprNewsType);
				Integer current;
				Integer size;
				Page page = null;
				try {
					current = Integer.parseInt(req.getParameter("page"));
					size = Integer.parseInt(req.getParameter("size"));
				} catch (NumberFormatException e) {
					current = 1;
					size = 10;
				}
				String sql = "type = " + key + " and";
				page = iprNewsServiceImpl.page(current, size, sql);
				page.setPageNo(current);
				req.setAttribute("lists", page);
				
				if (doer != null) {
					List<IprNewsInfo> list = iprNewsServiceImpl.listinfoByType(Integer.parseInt(key));
					req.setAttribute("list", list);
					if (id != null) {
						IprNewsInfo iprNewsInfo = iprNewsServiceImpl.findInfoById(Integer.parseInt(id));
						req.setAttribute("info", iprNewsInfo);
					}
				}
			}

			req.getRequestDispatcher("WEB-INF/ftl/Ipr/addIprNews.ftl").forward(req, resp);
		} else if (method.equals("add")) {
			String json = JsonUtil.getStrResponse(req);
			IprNewsInfoForm iprNewsInfoForm = new Gson().fromJson(json, IprNewsInfoForm.class);
			if (iprNewsServiceImpl.addNewsInfo(iprNewsInfoForm))
				resp.getWriter().println(JsonUtil.setData(ResultVOUtil.success()));
			else
				resp.getWriter().println(JsonUtil.setData(ResultVOUtil.failed(CommonEnums.PARAM_ERROR)));
		} else if (method.equals("edit")) {
			String json = JsonUtil.getStrResponse(req);
			NewsEditForm newsEditForm = new Gson().fromJson(json, NewsEditForm.class);
			if (iprNewsServiceImpl.edit(newsEditForm))
				resp.getWriter().println(JsonUtil.setData(ResultVOUtil.success()));
			else
				resp.getWriter().println(JsonUtil.setData(ResultVOUtil.failed(CommonEnums.PARAM_ERROR)));
		} else if (method.equals("delete")) {
			Integer id = Integer.parseInt(req.getParameter("id"));
			if (iprNewsServiceImpl.delete(id))
				resp.getWriter().println(JsonUtil.setData(ResultVOUtil.success()));
			else
				resp.getWriter().println(JsonUtil.setData(ResultVOUtil.failed(CommonEnums.PARAM_ERROR)));

		}else if (method.equals("deleteNewsItem")) {
			Integer id = Integer.parseInt(req.getParameter("id"));
			if (iprNewsServiceImpl.deleteNewsItem(id))
				resp.getWriter().println(JsonUtil.setData(ResultVOUtil.success()));
			else
				resp.getWriter().println(JsonUtil.setData(ResultVOUtil.failed(CommonEnums.PARAM_ERROR)));

		}
		
		else if (method.equals("addItem_page")) {
			List<String> showTypes = iprNewsServiceImpl.showTypes();
			List<String> showTypesId = iprNewsServiceImpl.showTypesId();
			req.setAttribute("types", showTypes);
			req.setAttribute("typesid", showTypesId);
			req.getRequestDispatcher("WEB-INF/ftl/Ipr/addIprNewsItem.ftl").forward(req, resp);
		} else if (method.equals("addItem")) {
			String json = JsonUtil.getStrResponse(req);
			System.out.println("json:" + json);
			IprNewsItemForm iprNewsItemForm = new Gson().fromJson(json, IprNewsItemForm.class);
			System.out.println("item:" + iprNewsItemForm.toString());
			if (iprNewsServiceImpl.addNewsItem(iprNewsItemForm))
				resp.getWriter().println(JsonUtil.setData(ResultVOUtil.success()));
			else
				resp.getWriter().println(JsonUtil.setData(ResultVOUtil.failed(CommonEnums.PARAM_ERROR)));

		} else if (method.equals("edit_page")) {
			String id = req.getParameter("id");
			id = id == null ? "1" : id;
			IprNewsItem iprNewsItem = iprNewsServiceImpl.findById(Integer.parseInt(id));
			req.setAttribute("news", iprNewsItem);
			req.getRequestDispatcher("WEB-INF/ftl/Ipr/editNews.ftl").forward(req, resp);
		} else if (method.equals("edit")) {
			String json = JsonUtil.getStrResponse(req);
			NewsEditForm newsEditForm = new Gson().fromJson(json, NewsEditForm.class);
			if (iprNewsServiceImpl.edit(newsEditForm))
				resp.getWriter().println(JsonUtil.setData(ResultVOUtil.success()));
			else
				resp.getWriter().println(JsonUtil.setData(ResultVOUtil.failed(CommonEnums.PARAM_ERROR)));
		} 
		return;
	}
}
